/*
 * @Author: zi.yang
 * @Date: 2021-09-29 00:13:21
 * @LastEditors: zi.yang
 * @LastEditTime: 2021-10-04 18:29:13
 * @Description: In User Settings Edit
 * @FilePath: \ziYangPM\src\store\index.js
 */
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
// import SecureLS from 'secure-ls'
Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

// 加密秘钥
// const ENCRYPTION_SECRET = '_ziYang@083654_G8K#504W@gjKt!C'
// const ls = new SecureLS({
//   encodingType: 'rc4',
//   isCompression: false,
//   encryptionSecret: ENCRYPTION_SECRET
// })

// 数据持久化
const persistedState = createPersistedState({
  key: '_ZiYang_PM_STORAGE_',
  // storage: {
  //   getItem: (key) => ls.get(key),
  //   setItem: (key, value) => ls.set(key, value),
  //   removeItem: (key) => ls.remove(key)
  // },
  storage: localStorage,
  paths: ['User.userinfo', 'User.loginTime']
})

// 导入模块
const modulesFiles = require.context('./', true, /index\.js$/)
const modules = modulesFiles.keys().reduce((module, modulePath) => {
  if (modulePath === './index.js') return module
  const newModule = module
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1').split('/')[0]
  const value = modulesFiles(modulePath)
  newModule[moduleName] = value.default
  return newModule
}, {})

export default new Vuex.Store({
  strict: debug,
  plugins: [persistedState],
  modules
})
